package com.gantang.gax.engine.utils;

public class StrUtils {
    /**
     * 如果替换操作非常简单（比如单个字符替换），直接使用 StringBuilder会更有效率。
     *
     * @param input
     * @param oldChar
     * @param newChar
     * @return
     */
    public static String replaceChar(String input, char oldChar, char newChar) {
        return replaceChar_pri(input, oldChar, newChar);
    }

    private static String replaceChar_pri(String input, char oldChar, char newChar) {
        StringBuilder builder = new StringBuilder(input.length());
        for (int i = 0; i < input.length(); i++) {
            char currentChar = input.charAt(i);
            if (currentChar == oldChar) {
                builder.append(newChar);
            } else {
                builder.append(currentChar);
            }
        }
        return builder.toString();
    }
}
